//
//  BVSettingViewController.m
//  BuyVegetable
//
//  Created by yushanchang on 16/10/4.
//  Copyright © 2016年 com.yinding. All rights reserved.
//

#import "BVSettingViewController.h"
#import "UIImageView+WebCache.h"
#import "BVResetPasswordViewController.h"
#import "BVUserInfoViewController.h"
#import "BVLoginViewController.h"
@interface BVSettingViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,strong)UITableView *tableView;
@property (nonatomic,strong)NSArray * arrayTitel;

@end

@implementation BVSettingViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"设置";
    
    [self setUI];
}

-(UITableView *)tableView{
    
    if (!_tableView) {
        
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, ScreenW, ScreenH-64) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.rowHeight = 45;
        _tableView.tableFooterView = [[UIView alloc] init];
        _tableView.scrollEnabled = NO;
        [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"iden"];
    }
    return _tableView;
}

-(void)setUI{
    
    [self.view addSubview:self.tableView];
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.arrayTitel.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"iden" forIndexPath:indexPath];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    
    cell.textLabel.text = self.arrayTitel[indexPath.row];
    cell.textLabel.textColor = [UIColor BVTextGrayColor];
    return cell;
    
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    
        if (indexPath.row == 0) {
            if (self.user.login) {
                BVUserInfoViewController * date = [[BVUserInfoViewController alloc]init];
                [self.navigationController pushViewController:date animated:YES];
            }else{
                [self showMessage:@"请先登录"];
            }
            
        }
        else if (indexPath.row == 1){
            if (self.user.login) {
                BVResetPasswordViewController * resetPassword = [[BVResetPasswordViewController alloc]init];
                [self.navigationController pushViewController:resetPassword animated:YES];

            }else{
                [self showMessage:@"请先登录"];
            }
        }else if (indexPath.row == 2){
            [self clearClick];
        }
}

//清缓存
- (void)clearClick {
    
    CGFloat cachesSize;// = [self getCachesSize];
    if(cachesSize <=0.001){
        UIAlertController * controller = [UIAlertController alertControllerWithTitle:@"" message:@"没有缓存" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction * sureAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil];
        [controller addAction:sureAction];
        [self presentViewController:controller animated:YES completion:nil];
    }
    else{
        UIAlertController * alertController = [UIAlertController alertControllerWithTitle:@"" message:[NSString stringWithFormat:@"清除缓存%.2fM",cachesSize] preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction * cancleAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
            
        }];
        
        UIAlertAction * sureAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            
            NSFileManager * fm = [NSFileManager defaultManager];
            NSString * path = [NSHomeDirectory() stringByAppendingPathComponent:@"Library/Caches"];
            NSArray * array = [fm contentsOfDirectoryAtPath:path error:nil];
            for(NSString * str in array)
            {
                NSString * newPath = [path stringByAppendingPathComponent:str];
                [fm removeItemAtPath:newPath error:nil];
            }
            
        }];
        
        [alertController addAction:cancleAction];
        [alertController addAction:sureAction];
        [self presentViewController:alertController animated:YES completion:nil];
        
    }
}
- (CGFloat ) folderSizeAtPath:(NSString*) folderPath{
    
    NSFileManager* manager = [NSFileManager defaultManager];
    if (![manager fileExistsAtPath:folderPath]) return 0;
    NSEnumerator *childFilesEnumerator = [[manager subpathsAtPath:folderPath] objectEnumerator];
    NSString* fileName;
    long long folderSize = 0;
    while ((fileName = [childFilesEnumerator nextObject]) != nil){
        NSString* fileAbsolutePath = [folderPath stringByAppendingPathComponent:fileName];
        folderSize += [self fileSizeAtPath:fileAbsolutePath];
    }
    // 图片缓存
    folderSize += [[SDImageCache sharedImageCache] getSize];
    return folderSize/(1024.0*1024.0);
}

//获取单个文件大小
- (long long) fileSizeAtPath:(NSString*) filePath{
    
    NSFileManager* manager = [NSFileManager defaultManager];
    if ([manager fileExistsAtPath:filePath]){
        return [[manager attributesOfItemAtPath:filePath error:nil] fileSize];
    }
    return 0;
}
-(NSArray *)arrayTitel{
    if (!_arrayTitel) {
        _arrayTitel = [NSArray arrayWithObjects:@"资料设置",@"修改登录密码",@"清楚缓存", nil];
    }
    return _arrayTitel;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
